$input v_texcoord0, v_color, horizioncolor, cloudscolor, alfadata, worldpos

#include <bgfx_shader.sh>
#include <Fuctions_MLYX.glsl>

SAMPLER2D(s_MatTexture, 0);

uniform vec4 FogAndDistanceControl;
uniform vec4 ViewPositionAndTime;
uniform vec4 FogColor;

highp float RSPCT(highp vec2 RZ){return fract(cos(RZ.x+RZ.y*1024.0)*1024.0);}
highp float shaderra(highp vec2 RZ){ 
highp vec2 R1=floor(RZ);highp vec2 R2 = fract(RZ);highp vec2 R3 = pow(R2,vec2(3.0,3.0))*(3.0-2.0*R2); 
return mix(mix(RSPCT(R1+vec2(0.0,0.0)),RSPCT(R1+vec2(1.0,0.0)),R3.x),mix(RSPCT(R1+vec2(0.0,1.0)),RSPCT(R1+vec2(1.0,1.0)),R3.x),R3.y);}

highp float rand(highp vec2 RR){return fract(cos(RR.x+RR.y*256.0)*256.0);}
highp float Noiz(highp vec2 RR){ 
highp vec2 UNUM =floor(RR);highp vec2 DOSI =fract(RR);highp vec2 TREZ =pow(DOSI,vec2(3.0,3.0))*(3.0- 2.0*DOSI); 
highp float CUAT =rand(UNUM+vec2(0.0,0.0));highp float CING = rand(UNUM+vec2(1.0,0.0));highp float CINC = rand(UNUM+vec2(0.0,1.0));highp float CINT = rand(UNUM+vec2(1.0,1.0));
highp float RR1=mix(mix(CUAT,CING,TREZ.x),mix(CINC,CINT,TREZ.x),TREZ.y);return RR1;}

highp float nubesmaping(highp vec2 Locatioz, float TIME){
highp float UNUM=1.0;highp float DOSI=0.0;
int TREZ=0;Locatioz+=vec2(2.0,1.0)*TIME*0.000;while(TREZ<6){
DOSI+= shaderra(Locatioz)/UNUM;
UNUM*=1.8;
Locatioz*=2.4;
Locatioz+= TIME*0.00*pow(UNUM,0.1);
Locatioz+= shaderra(Locatioz+TIME*0.0)/UNUM;
TREZ++;
}
return pow(abs(DOSI+0.4),5.0);}


vec4 Normalnubimixum(vec3 locati,float diez,float ragnarokn,float luna,float blue, float TIME) {
vec4 nubediez=vec4(1.3,1.26,1.2,1.3);
vec4 nubeluna=vec4(0.5,0.5,0.5,0.5);
vec4 nuberagnarok=vec4(1.8,1.1,0.3,1.0);
vec4 nubeblue=vec4(0.6,0.6,0.6,1.0);
vec4 nubeapi=mix(nubediez,nuberagnarok,ragnarokn*(1.0-blue));nubeapi=mix(nubeapi,nubeluna,luna);nubeapi=mix(nubeapi,nubeblue,blue);

float anube=nubesmaping(locati.xz, TIME);

vec4 nuberagna=mix(vec4(1.0,1.0,1.0,1.0),nubeapi*0.5,0.5+ragnarokn);nuberagna=mix(vec4(1.0,1.0,1.0,1.0),nubeapi*0.01, luna);
nuberagna.rgb=mix(nuberagna.rgb,nubeapi.rgb*0.8,anube);nuberagna.a=mix(anube,0.0,blue);
return nuberagna;}

void main() {
    vec4 diffuse = texture2D(s_MatTexture, v_texcoord0);
    vec4 final;
    float time = ViewPositionAndTime.w;
    final = vec4(1.0,1.0,1.0,0.0);
    float HorizionCaelo = max( 1.0 - length(worldpos.xz / worldpos.y ) * 0.02, 0.0 );
    
    vec4 GraghicNubes = Normalnubimixum(( worldpos.xyz/worldpos.y )*1.0,1.0,0.0,0.0,0.0,time);//云图
    bool InsuperiAquoris = FogAndDistanceControl.x==0.0&&FogAndDistanceControl.y<0.8&&(FogColor.b>FogColor.r||FogColor.g>FogColor.r);
    
    if(InsuperiAquoris){
        final = vec4(horizioncolor,0.0);
    }else{
        final = mix( final, vec4(cloudscolor,1.0), GraghicNubes.a * mix(0.0, alfadata, clamp(pow4(HorizionCaelo), 0.0, 1.0)));
    }
    
    final = mix( final, vec4(horizioncolor,1.0), ( worldpos.y > 0.0 ? 1.0 : 0.0 ));

    final.rgb = pow(final.rgb, vec3(2.0,2.0,2.0));
    final.rgb = ACESToneMapping( final.rgb, 1.0 );
    final.rgb = pow(final.rgb, vec3(1.0 / 2.2, 1.0 / 2.2, 1.0 / 2.2));

    #define CaeloEst(diffuse) (diffuse.r<0.01 && diffuse.g<0.01 && diffuse.b<0.01)
    if(CaeloEst(diffuse)){
        diffuse = final;
    }
    //diffuse = mix(diffuse, final, diffuse.a * diffuse.r>0.99?1.0:0.0);

    gl_FragColor = diffuse;
}
